package com.yanqu.road.server.manger.system;

import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.login.LoginProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.ArrayList;
import java.util.List;

public class ActivityIgnoreMgr extends TempMgr {

    private static List<Integer> activityLimitList = new ArrayList<>();
    private static LoginProto.SystemActivityIgnoreMsg msg;

    @Override
    public boolean reloadData() throws Exception {
        try {
            activityLimitList = ActivityBussiness.getActivityIgnoreList(GameServer.getInstance().getServerId());
            LoginProto.SystemActivityIgnoreMsg.Builder tempMsg = LoginProto.SystemActivityIgnoreMsg.newBuilder();
            if (activityLimitList != null && activityLimitList.size() > 0) {
                tempMsg.addAllActivityIdList(activityLimitList);
                getLogger().info("屏蔽活动{}",activityLimitList);
            }
            msg = tempMsg.build();
            syncAllActivityIgnore();
        } catch (Exception e) {
            getLogger().error("reload", e);
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 后台刷新配置时同步
     */
    public static void syncAllActivityIgnore() {
        if (msg != null) {
            List<GamePlayer> copyPlayerList = GamePlayerMgr.getCopyPlayerList();
            getLogger().info("屏蔽活动同步玩家数{}",copyPlayerList.size());
            for (GamePlayer player : copyPlayerList) {
                player.sendPacket(ClientProtocol.U_SYSTEM_ACTIVITY_IGNORE, msg.toBuilder());
            }
        }
    }

    /**
     * 玩家登陆时同步
     *
     * @param player
     */
    public static void syncActivityIgnore(GamePlayer player) {
        try {
            if (msg != null) {
                player.sendPacket(ClientProtocol.U_SYSTEM_ACTIVITY_IGNORE, msg.toBuilder());
            }
        } catch (Exception e) {
            getLogger().error("syncActivityIgnore", e);
        }
    }
}
